//
//  ProductsFilterParamView.swift
//  weile
//
//  Created by LKM on 16/10/19.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit

protocol ProductsFilterParamViewBC {
    func onClickItem(_ v:ProductsFilterParamView,_ data:ProductsToFilter2,_ index:Int)
}
/**
 筛选的选择参数列表
 */
class ProductsFilterParamView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    override func awakeFromNib() {
        super.awakeFromNib()
        
        //xib 过来 保证是关闭的
        close();
    }
    var delegate:ProductsFilterParamViewBC?;
    
    fileprivate var itemW=0;
    private var isInitLayoutSubviews=false;
    override func layoutSubviews() {
        super.layoutSubviews();
        if(isInitLayoutSubviews){
            return;
        }
        isInitLayoutSubviews=true;
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .vertical
        
        //10是最后一个的下面空隙
        // 一排 3个太多 4个又放不下。这时会放3个  然后剩下的平分
        let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.width, height: 0), collectionViewLayout: flowLayout)
        collectionView.backgroundColor=R.colors.bg_global
        collectionView.delegate=self
        collectionView.dataSource=self;//Adapter
        collectionView.showsHorizontalScrollIndicator=false;//不显示滚动条？
        self.collectionView=collectionView;
//        collectionView.backgroundColor=UIColor.white;
        
        let cellNib = UINib(nibName: "ProductsFilterParamItem", bundle: nil)
        collectionView.register(cellNib, forCellWithReuseIdentifier: "cell")
        addSubview(collectionView)

        backgroundColor=R.colors._25000000
        
    }
    
    var collectionView:UICollectionView!;
    var collections :[ProductsToFilter2]?
    
    func binData(filter2s:[ProductsToFilter2]?){
        if filter2s == nil{
            return;
        }
        collections = filter2s;
        
        let lie=3;
        
        let space = 10;
        
        let han = ceil( Double(collections!.count) / Double(lie) )
        
        itemW = (Int(self.width)-space*(lie+1)  )/lie;//10是空隙
        
        ViewHelp.setViewHeigth(collectionView, min(self.height-MainTabBarHeight,CGFloat( (space*(lie+1)) * Int(han))+CGFloat(space) ) )
        
        collectionView.reloadData()
    }
    
    func close(){
        isHidden=true;
    }
    func show(){
        isHidden=false;
    }
    
}
extension ProductsFilterParamView:UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    
    //头部  点击更多
    func topViewDidClickedMoreButton(_ button: UIButton) {
        
    }
    
    // MARK: - UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return collections?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ProductsFilterParamItem
//        cell.btn_title.setText( collections?[(indexPath as NSIndexPath).item].name ?? "")
//        cell.collection = collections[(indexPath as NSIndexPath).item]
        cell.binData((collections?[(indexPath as NSIndexPath).item])!)
        return cell
    }
    
    // MARK: - UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let index=(indexPath as NSIndexPath).item
        let data:ProductsToFilter2=(collections?[index])!
        
        //单选的
        for d in collections!{
            d.isSelect=false;
        }
        data.isSelect = !data.isSelect;
        
        collectionView.reloadData()
        
        delegate?.onClickItem(self, data, index)
        
        //ViewHelp.showTipsView(containView: self.viewController()!.view, tips: data.name)
        
    }
    
    // MARK: - UICollectionViewDelegateFlowLayout
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        //        /// 分类界面 顶部 item 的高
        //        let kitemH: CGFloat = 75
        //        /// 分类界面 顶部 item 的宽
        //        let kitemW: CGFloat = 150
        return CGSize(width: itemW, height: 30)
    }
    
    //注释掉也是行的。但是 大的四周没空隙了 会撑开
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let kMargin:CGFloat=10.0
        return UIEdgeInsetsMake(kMargin, kMargin, kMargin, kMargin)
    }
    
    
}
